﻿using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.DataAccess;
using BySoft.Server.DataAccess.UserCode;
using OneSoft.DataAccess;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BySoft.Server.Business.UserCode
{
    public partial class BsScheduleBusniess
    {
        public static DataTable SelectDialogList(string ScheduleID,string ProductID, string ProductName, string Specifications, string Unit, string OutSide, string InSide, int pageIndex, int pageSize, out int m_count)
        {
            return BsScheduleDataAccess.SelectDialogList(ScheduleID,ProductID, ProductName, Specifications, Unit, OutSide, InSide, pageIndex, pageSize, out m_count);
        }

        public static bool IsScheduleIDExists(string ScheduleID)
        {
            bool isResult = false;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    string str = @"select * from BS_Schedule where ScheduleID=@ScheduleID";
                    DataAccessParameterCollection dpc = new DataAccessParameterCollection();
                    dpc.AddWithValue("@ScheduleID", ScheduleID);
                    DataTable d = DataAccessCommon.Select(str, dpc).Tables[0];
                    if (d != null && d.Rows.Count > 0)
                    {
                        isResult = true;
                    }
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = true;
                    broker.Rollback();
                    throw ex;
                }
            }
            return isResult;
        }

        public static bool SaveOrUpdate(BsScheduleEntity entity, DataTable ScheduleDetailDT, DataTable ScheduleProcessDT, DataTable ScheduleMachineDT, string strType)
        {
            bool istag = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    if (strType == "Save")
                    {
                        DataAccessCommon.Insert(entity, broker);
                    }
                    else if (strType == "Update")
                    {
                        DataAccessCommon.UpdateOnly(entity, broker);
                    }
                    //产品
                    if (ScheduleDetailDT != null && ScheduleDetailDT.Rows.Count > 0)
                    {
                        foreach (DataRow drItem in ScheduleDetailDT.Rows)
                        {
                            BsScheduledetailEntity DetailEntity = new BsScheduledetailEntity();
                            DetailEntity.ScheduleID = entity.ScheduleID;
                            DetailEntity.WorkOrderNo = CommonMethod.GetString(drItem["WorkOrderNo"]);
                            DetailEntity.PlanID = CommonMethod.GetString(drItem["PlanID"]);
                            DetailEntity.ScheduleNum = CommonMethod.ConvertToDecimal(drItem["ScheduleNum"]);
                            DetailEntity.Remark = CommonMethod.GetString(drItem["Remark"]);
                            DetailEntity.ExpectInStockDate = Convert.ToDateTime(drItem["ExpectInStockDate"]);
                            string strIsNew = CommonMethod.GetString(drItem["IsNew"]);
                            string strIsDel = CommonMethod.GetString(drItem["IsDel"]);
                            decimal ScheduleDetailID = 0;
                            if (strIsNew == "1" && strIsDel != "1")
                            {
                                ScheduleDetailID = CommonMethod.ConvertToDecimal(DataAccessCommon.GetInsert(DetailEntity, true, broker));
                                if (ScheduleDetailID > 0)
                                {
                                    DataTable dt = BsScheduleDataAccess.GetBomDetailByWorkOrderNo(DetailEntity.WorkOrderNo);
                                    foreach (DataRow dr in dt.Rows)
                                    {
                                        BsSchedulematEntity en = new BsSchedulematEntity();
                                        en.ScheduleDetailID = ScheduleDetailID;
                                        en.BOMDetailID = CommonMethod.ConvertToDecimal(dr["BOMDetailID"]);
                                        en.MaterialID = CommonMethod.GetString(dr["MaterialID"]);
                                        en.TotalNum = DetailEntity.ScheduleNum * CommonMethod.ConvertToDecimal(dr["MMNumber"]);
                                        en.TotalWeight = DetailEntity.ScheduleNum * CommonMethod.ConvertToDecimal(dr["SingleWeight"]);
                                        DataAccessCommon.Insert(en, true, broker);
                                    }
                                }
                            }
                            if (strIsNew != "1" && strIsDel != "1")
                            {
                                ScheduleDetailID = CommonMethod.ConvertToDecimal(drItem["ScheduleDetailID"]);
                                DetailEntity.ScheduleDetailID = CommonMethod.ConvertToDecimal(drItem["ScheduleDetailID"]);
                                DataAccessCommon.UpdateOnly(DetailEntity, broker);
                                if (DetailEntity.ScheduleDetailID > 0)
                                {
                                    DataTable dt = BsScheduleDataAccess.GetBomDetailByWorkOrderNo(DetailEntity.WorkOrderNo);
                                    foreach (DataRow dr in dt.Rows)
                                    {
                                        BsSchedulematEntity en = new BsSchedulematEntity();
                                        en.ScheduleDetailID = DetailEntity.ScheduleDetailID;
                                        en.BOMDetailID = CommonMethod.ConvertToDecimal(dr["BOMDetailID"]);
                                        en.MaterialID = CommonMethod.GetString(dr["MaterialID"]);
                                        en.TotalNum = DetailEntity.ScheduleNum * CommonMethod.ConvertToDecimal(dr["MMNumber"]);
                                        en.TotalWeight = DetailEntity.ScheduleNum * CommonMethod.ConvertToDecimal(dr["SingleWeight"]);
                                        DataAccessCommon.UpdateOnly(en, broker);
                                    }
                                }
                            }
                            if (strIsNew != "1" && strIsDel == "1")
                            {
                                ScheduleDetailID = CommonMethod.ConvertToDecimal(drItem["ScheduleDetailID"]);
                                ClsCommDA.Del("BS_ScheduleMat", "ScheduleDetailID", CommonMethod.GetString(drItem["ScheduleDetailID"]), broker);
                                //删除明细
                                ClsCommDA.Del("BS_ScheduleDetail", "ScheduleDetailID", CommonMethod.GetString(drItem["ScheduleDetailID"]), broker);
                            }





                            //工序
                            if (ScheduleProcessDT != null && ScheduleProcessDT.Rows.Count > 0)
                            {
                                DataTable MachineDT = ScheduleMachineDT.Copy();
                                foreach (DataRow drProcess in ScheduleProcessDT.Rows)
                                {
                                    decimal ordernum = CommonMethod.ConvertToDecimal(drProcess["OrderNum"]);
                                    BsScheduleprocessEntity DetailE = new BsScheduleprocessEntity();
                                    DetailE.ScheduleDetailID = ScheduleDetailID;
                                    DetailE.OrderNum = ordernum;
                                    DetailE.ProcessID = CommonMethod.GetString(drProcess["ProcessID"]);
                                    DetailE.IsOutSource = CommonMethod.GetString(drProcess["IsOutSource"]);
                                    DetailE.IsCheck = CommonMethod.GetString(drProcess["IsCheck"]);
                                    string strIsNew1 = CommonMethod.GetString(drProcess["IsNew"]);
                                    string strIsDel1 = CommonMethod.GetString(drProcess["IsDel"]);
                                    decimal ScheduleProcessID = 0;


                                    DataRow[] drs = MachineDT.Select("ProcessID='" + DetailE.ProcessID.Trim() + "'");
                                    BsSchedulemachineEntity mac = new BsSchedulemachineEntity();
                                    if (drs.Length > 0)
                                    {
                                        DataRow dr = drs[0];
                                        mac.BillDate = Convert.ToDateTime(dr["BillDate"]);
                                        mac.ProcessID = CommonMethod.GetString(dr["ProcessID"]);
                                        mac.MachineID = CommonMethod.GetString(dr["MachineID"]);
                                        mac.ExpectInputTime = CommonMethod.ConvertToDecimal(dr["ExpectInputTime"]);
                                        mac.SingleStandardProcessTime = CommonMethod.ConvertToDecimal(dr["SingleStandardProcessTime"]);
                                        mac.AdjustTime = CommonMethod.ConvertToDecimal(dr["AdjustTime"]);
                                        mac.SingleClampTime = CommonMethod.ConvertToDecimal(dr["SingleClampTime"]);
                                        mac.ExpectProduceTime = Convert.ToDateTime(dr["ExpectProduceTime"]);
                                    }


                                    if (strIsNew1 == "1" && strIsDel1 != "1" && ordernum > 0)
                                    {
                                        ScheduleProcessID = DataAccessCommon.GetInsert(DetailE, true, broker);
                                        if (mac != null)
                                        {
                                            mac.ScheduleProcessID = ScheduleProcessID;
                                            DataAccessCommon.Insert(mac, true, broker);
                                        }
                                    }
                                    if (strIsNew1 != "1" && strIsDel1 != "1" && ordernum > 0)
                                    {
                                        ScheduleProcessID = CommonMethod.ConvertToDecimal(drProcess["ScheduleProcessID"]);
                                        DetailE.ScheduleProcessID = ScheduleProcessID;
                                        DataAccessCommon.UpdateOnly(DetailE, broker);

                                        if (mac != null)
                                        {
                                            mac.ScheduleProcessID = ScheduleProcessID;
                                            DataAccessCommon.UpdateOnly(mac, broker);
                                        }
                                    }
                                    if (strIsNew1 != "1" && strIsDel1 == "1" || ordernum <= 0)
                                    {
                                        //删除机台明细
                                        ClsCommDA.Del("BS_ScheduleMachine", "ScheduleProcessID", CommonMethod.GetString(drProcess["ScheduleProcessID"]), broker);
                                        //删除明细
                                        ClsCommDA.Del("BS_ScheduleProcess", "ScheduleProcessID", CommonMethod.GetString(drProcess["ScheduleProcessID"]), broker);
                                    }
                                }
                            }
                        }
                    }

                    broker.Commit();
                }
                catch
                {
                    istag = false;
                    broker.Rollback();
                    throw;
                }
                return istag;
            }
        }

        public static bool Del(string strCode)
        {
            bool isResult = true;
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    //机台
                    DataTable dtMac = BsScheduleMachineDataAccess.GetMachineByScheduleID(strCode);
                    foreach (DataRow dr in dtMac.Rows)
                    {
                        DataAccessCommon.Delete(CommonMethod.GetString(dr["ScheduleProcessID"]), "ScheduleProcessID", "BS_ScheduleMachine");
                    }
                    DataTable dtMat = BsScheduledetailDA.GetDSByCondition("ScheduleID='" + strCode + "'").Tables[0];
                    foreach (DataRow dr in dtMat.Rows)
                    {
                        //工序
                        DataAccessCommon.Delete(CommonMethod.GetString(dr["ScheduleDetailID"]), "ScheduleDetailID", "BS_ScheduleProcess");
                        //原料表
                        DataAccessCommon.Delete(CommonMethod.GetString(dr["ScheduleDetailID"]), "ScheduleDetailID", "BS_ScheduleMat");
                    }
                    //产品明细表
                    DataAccessCommon.Delete(strCode, "ScheduleID", "BS_ScheduleDetail");
                    //主表
                    DataAccessCommon.Delete(strCode, "ScheduleID", "BS_Schedule");
                    broker.Commit();
                }
                catch (Exception ex)
                {
                    isResult = false;
                    broker.Rollback();
                    throw ex;
                }
            }
            return isResult;
        }

        public static DataTable SelectList(string ScheduleID, string From, string End, int pageIndex, int pageSize, out int m_count)
        {
            return BsScheduleDataAccess.SelectList(ScheduleID, From, End, pageIndex, pageSize, out m_count);
        }

        public static DataTable GetScheduleHeadByID(string ScheduleID)
        {
            return BsScheduleDataAccess.GetScheduleHeadByID(ScheduleID);
        }

        public static DataTable GetScheduleBodyProcessByID(string ScheduleDetailID)
        {
            return BsScheduleDataAccess.GetScheduleBodyProcessByID(ScheduleDetailID);
        }

        public static DataTable GetMachineDataTable()
        {
            return BsScheduleDataAccess.GetMachineDataTable();
        }

        public static DataTable GetBomDetailByWorkOrderNo(string WorkOrderNo)
        {
            return BsScheduleDataAccess.GetBomDetailByWorkOrderNo(WorkOrderNo);
        }
    }
}
